package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.CommentBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.CommentMapper;
import com.liuzu.esys.pojo.Comment;
import com.liuzu.esys.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("/commentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public LayuiPageVo<Comment> commentPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);
        List<Comment> comments = commentMapper.selectByParams(params);

        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Comment> commentLayuiPageVo = new LayuiPageVo<>();
        commentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        commentLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        commentLayuiPageVo.setCount(commentPageInfo.getTotal()); // 总记录数
        commentLayuiPageVo.setData(commentPageInfo.getList());  // 分页的列表数据
        return commentLayuiPageVo;
    }

    @Override
    public int commentAdd(Comment comment) { return commentMapper.commentAdd(comment); }

    @Override
    public int commentUpdate(Comment comment) { return commentMapper.commentUpdate(comment); }

    @Override
    public int commentDelete(int order_number) { return commentMapper.commentDelete(order_number); }

    @Override
    public List<CommentBo> commentExcelData(Comment comment) {
        List<Comment> comments = commentMapper.selectBykey(comment);
        List<CommentBo> commentBos = comments.stream()
                .map((dr) -> {
                    CommentBo commentBo = new CommentBo();
                    commentBo.setComment_content(dr.getComment_content());
                    commentBo.setName(dr.getName());
                    commentBo.setUser_name(dr.getUser_name());
                    commentBo.setComment_time(dr.getComment_time());
                    commentBo.setOrder_number(dr.getOrder_number());
                    return commentBo;
                }).collect(Collectors.toList());
        return commentBos;
    }
}
